package com.kingt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Office2PDFTest {
    public static void main(String[] args) {
//		//运行之前需要在F:\office下新建MVC.docx,当然也可以换成是其他的路径
//		String sourceFile = "F:\\office\\problems.docx";
//		String destFile = "F:\\office\\MVC.pdf";
//
//        //return 1,转换失败。记录下失败的文件
//
//		System.out.println(Office2PDF.office2PDF(sourceFile, destFile));


        Properties prop = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream( Thread.currentThread()
                    .getContextClassLoader().getResource("").getPath()
                    .replace("%20", " ")
                    + "url.properties");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 属性文件输入流
        try {
            prop.load(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 将属性文件流装载到Properties对象中
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 关闭流

        String dir = prop.getProperty("dir");
        traverseFolder(dir);
    }

    public static void traverseFolder(String path) {

        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        //System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder(file2.getAbsolutePath());
                    } else {
                        //System.out.println("文件:" + file2.getAbsolutePath());
                        String filePath = file2.getAbsolutePath();
                        String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length()).toLowerCase();
                        if(fileType.equals("doc")||fileType.equals("docx")){
                            //将word文件转为pdf
                            String destFile = filePath.substring(0,filePath.lastIndexOf("."))+".pdf";
                            //如果没有同名的pdf才要做转换
                            if(!new File(destFile).exists()){
                                if(Office2PDF.office2PDF(filePath, destFile)!=0){
                                    //记录转换错误的文件路径
                                    try {
                                        logWriter(filePath);
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }//end if 没有同名的pdf才要做转换
                        }//end if 是world文件
                    }
                }//end for files
            }//if else exits file or directory
        } else {
            System.out.println("文件不存在!");
        }
    }

    public  static  void logWriter(String content) throws IOException {
        File  f =new File("e:\\Office2PDF.txt");
        // 创建文件
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream out  = new FileOutputStream(f,true);
        out.write((content+"\r\n").getBytes("utf-8"));
        out.close();
    }

}
